using System;
using System.Collections.Generic;

namespace SG3L_RAG.Domain.Entities
{
    public class User : EntityBase
    {
    public string Username { get; private set; } = string.Empty;
    public string Password { get; private set; } = string.Empty;
    public string Email { get; private set; } = string.Empty;
    public string PhoneNumber { get; private set; } = string.Empty;
    public string Role { get; private set; } = string.Empty;
    public string? Avatar { get; private set; } = string.Empty; // 头像URL
        private readonly List<Document> _documents = new();
        public IReadOnlyCollection<Document> Documents => _documents.AsReadOnly();
        private readonly List<Conversation> _conversations = new();
        public IReadOnlyCollection<Conversation> Conversations => _conversations.AsReadOnly();
        private readonly List<Permission> _permissions = new();
        public IReadOnlyCollection<Permission> Permissions => _permissions.AsReadOnly();

        // EF Core 需要的无参构造函数
        public User() { }

        // 业务构造函数（含手机号）
        private User(string username, string password, string email, string phoneNumber, string role)
        {
            Username = username;
            Password = password;
            Email = email;
            PhoneNumber = phoneNumber;
            Role = role;
            CreatedAt = DateTime.UtcNow;
        }

        // 工厂方法（含手机号）
        public static User Create(string username, string password, string email, string phoneNumber, string role)
        {
            return new User(username, password, email, phoneNumber, role);
        }

        // 兼容旧接口（无手机号）
        public static User Create(string username, string password, string email, string role)
        {
            return new User(username, password, email, string.Empty, role);
        }

        public bool CheckPassword(string inputPassword)
        {
            // 实际项目应加密校验
            return inputPassword == Password;
        }

        public bool IsAdmin() => Role == "admin";
        public bool IsNormalUser() => Role == "user";

        public void AddDocument(Document doc) => _documents.Add(doc);
        public void AddConversation(Conversation conv) => _conversations.Add(conv);
        public void AddPermission(Permission perm) => _permissions.Add(perm);

        public bool HasPermission(Document doc, string type)
        {
            foreach (var p in _permissions)
            {
                if (p.DocumentId == doc.Id && p.PermissionType == type)
                    return true;
            }
            return false;
        }

        // 充血模型补充：更新手机号
        public void UpdatePhoneNumber(string phoneNumber)
        {
            PhoneNumber = phoneNumber ?? string.Empty;
        }

        // 充血模型补充：更新邮箱
        public void UpdateEmail(string email)
        {
            Email = email ?? string.Empty;
        }

        // 充血模型补充：更新用户名
        public void UpdateUsername(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
                throw new ArgumentException("用户名不能为空");
            Username = username;
        }

        // 充血模型补充：更新角色
        public void UpdateRole(string role)
        {
            if (string.IsNullOrWhiteSpace(role))
                throw new ArgumentException("角色不能为空");
            Role = role;
        }

        // 充血模型补充：修改密码
        public void ChangePassword(string newPassword)
        {
            if (string.IsNullOrWhiteSpace(newPassword))
                throw new ArgumentException("密码不能为空");
            Password = newPassword;
        }

        // 充血模型补充：更新头像
        public void UpdateAvatar(string? avatar)
        {
            Avatar = avatar;
        }
    }
}
